import { InternalAxiosRequestConfig } from 'axios';
import { Encrypt } from '@/utils/crypto-data';

function seralizeOrdered(
  params: { timestamp: number; data: string; sign: string },
  splitStr = '&'
) {
  return (
    `timestamp=${params.timestamp}` +
    splitStr +
    `data=${params.data}` +
    splitStr +
    `sign=${params.sign}`
  );
}

export default function (
  request: InternalAxiosRequestConfig
): InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig> {
  if (request.data) {
    const debug = import.meta.env.VITE_DEBUG;

    if (import.meta.env.DEV && debug) {
      request.data = { ...request.data, debug };
      console.log(`[request]${request.url}`, request.data);
    } else {
      console.log(`[request]${request.url}`, request.data);
      request.data = seralizeOrdered(Encrypt({ ...request.data }));
    }
  }

  return request;
}
